package autoTest.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import autoTest.Holder;
import autoTest.command.ErrorCode;
import autoTest.command.Params;
import autoTest.database.DataElement;
import autoTest.database.DataManager;
import autoTest.utils.TextUtils;
import main.ServerMain;
import net.sf.json.JSONObject;

public class EndPart extends RequestPart {
	private static final long serialVersionUID = 2271797150647771294L;
	public EndPart() {} 
      
	protected void doResponse(HttpServletResponse resp, int code, String taskId) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");  
		resp.setContentType("text/plain");
		resp.setStatus(HttpServletResponse.SC_OK);
		PrintWriter pWriter = resp.getWriter();
		JSONObject joRet = new JSONObject();
		joRet.put(Params.CODE, code);
		joRet.put(Params.TASK_ID, taskId);
		pWriter.println(joRet.toString());  
	}

	@Override public void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	String deviceIdListStr = req.getParameter(Params.DEVICE_ID_LIST);
    	String taskIdStr = req.getParameter(Params.TASK_ID);
    	
    	System.out.println("[EndPart] taskIdStr=" + taskIdStr + ",deviceIdListStr=" + deviceIdListStr);
    	if(TextUtils.isEmpty(taskIdStr)) {
    		System.out.println("[EndPart] params be empty");
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR, null);
    		return;
    	}
    	
    	List<String> allDevice = new ArrayList<String>();			//所有需要发送任务的设备
    	if (!TextUtils.isEmpty(deviceIdListStr)) {		//提供了设备ID，收集所有的设备ID
	    	for (String ele : deviceIdListStr.split(",")) {
	    		allDevice.add(ele);
	    	}
    	} else {
    		for (String ele : ServerMain.allDeviceInfo.keySet()) {		//未提供设备ID，当前正在连接的设备
    			allDevice.add(ele);
    		}
    	}
    	
    	for (String ele : allDevice) {
    		System.out.println("[EndPart] step ele:" + ele);
    		if (!ServerMain.allDeviceInfo.keySet().contains(ele)) {		//当前requestId不包含收集到的设备ID，认为请求错误
    			System.out.println("[EndPart] no device can be stop");
    			doResponse(resp, ErrorCode.ERR_PARAM_ERROR, null);
    			return;
    		}
    	}
    	
    	String taskId = "task-" + System.currentTimeMillis();
    	DataElement element = new DataElement();
    	String deviceIDS = new String("");
    	for (String ele : allDevice) {
    		System.out.println("[EndPart]" + ServerMain.allDeviceReqMap.get(ele));
    		deviceIDS = deviceIDS + ele + ",";		//设备名数组化，用于存库
     	    Holder holder = ServerMain.allReponseMap.get(ServerMain.allDeviceReqMap.get(ele));
     	    holder.getListener().makeIt(1, null, taskId, taskIdStr);
     	    holder.getContinuation().complete();
    	}
    	element.setDeviceSet(deviceIDS.substring(0, deviceIDS.length() - 1));
    	element.setTaskId(taskId);
    	DataManager.putResult(element);
    	doResponse(resp, ErrorCode.SUCCESS, taskId);
	}
}
